@echo off
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
rem  Package:		OurGrid 4.2
rem  Description:	This batch script is used to create configure
rem                 and configure a Virtual Box Virtual Machine.
rem  Copyright (c)  2010 Universidade Federal de Campina Grande 
rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

if %1.==/?. goto usage
if %3.==. goto usage
goto findself

:findself
set SELF=%~d0%~p0
goto main

:main
set VMANAGE=%1
set VMNAME=%2
set DISK=%3
set MEM=%4

echo Creating Virtual Machine
%VMANAGE% createvm --name %VMNAME% --register --ostype Debian 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Registering disk
%VMANAGE% registerimage disk %DISK% -type immutable 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

%VMANAGE% storagectl %VMNAME% --name "SATA Controller" --add sata --controller IntelAhci 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

%VMANAGE% storageattach %VMNAME% --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium %DISK% 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Setting memory
%VMANAGE% modifyvm %VMNAME% --memory %MEM% 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Optimizing boot time. (STEP 1/3)
%VMANAGE% modifyvm %VMNAME% --bioslogofadein off 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Optimizing boot time. (STEP 2/3)	
%VMANAGE% modifyvm %VMNAME% --bioslogofadeout off 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Optimizing boot time. (STEP 3/3)
%VMANAGE% modifyvm %VMNAME% --biosbootmenu disabled 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Removing network.
%VMANAGE% modifyvm %VMNAME% --nic1 none 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Starting Image.
%VMANAGE% startvm %VMNAME% 1> nul
if not %ERRORLEVEL% == 0 (
	echo Unable to create machine 1>&2
	exit /B 1
)

echo Stop Image.
%VMANAGE% controlvm %VMNAME% poweroff 1> nul

goto eof

:usage
echo usage:
echo Usage $0 ^<VBoxManage location^> ^<Machine name^> ^<Virtual disk^> [Virtual machine memory] [-h]
echo where mandatory parameters are:
echo       VBoxManage location: Is the location of VBoxManage command
echo       Image name: Is the name of the virtual machine
echo       Virtual disk: Is the location of the virtual disk to use
echo and the optional parameters are:
echo       Virtual Machine Memory : Amount of memory to allocate to the VM in mega bytes. Default is 128.

:eof